<template>
	<view>
		<view class="box">
			<view class="aa">
				<view class="shop" @click.stop="shopIndex(detail.shop_id)">
					<image :src="baseurl+'/applet/common/shop.png'" mode="widthFix"></image>
					<view>{{detail.shopinfo.shopname}}</view>
					<image :src="baseurl+'/applet/common/newright.png'" mode="widthFix"></image>
				</view>
				<view class="state">已完成</view>
			</view>
			<view class="bb" v-for="(item,index) in detail.ordergoods">
				<image :src="item.image" mode="aspectFill"></image>
				<view class="detail">
					<view class="time">下单：{{detail.createtime}}</view>
					<view class="two">
						<view>价格：￥{{item.actual_payment}}</view>
						<view>共{{item.number}}件</view>
					</view>
				</view>
			</view>
			<view class="cc">
				<view class="t-name">退款金额</view>
				<view class="prices">￥{{detail.payinfio.actual_payment}}</view>
			</view>
		</view>
		<view class="tk-box">
			<view class="tk-head">
				<view class="names">退款原因</view>
				<picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
					<view class="rcon">
						<view class="uni-input" v-if="index>-1">{{array[index].name}}</view>
						<view class="mo" v-else>请选择退款原因</view>
						<image :src="baseurl+'/applet/common/newright.png'" mode="widthFix"></image>
					</view>
				</picker>
			</view>
			<view class="names">退款说明</view>
			<view class="ms">
				<textarea placeholder="补充描述，有助于更好的处理售后问题" v-model="content"></textarea>
				<view>{{content.length}}/100</view>
			</view>
			<view class="names">上传凭证</view>
			<view class="imgs">
				<view class="pz" @click="upload">
					<image :src="baseurl+'/applet/common/xiangji.png'" mode="aspectFill"></image>
				</view>
				<view class="add-img" v-for="(img,ci) in tmpimg" :key="ci">
					<view class="delete" @click="close(ci)">x</view>
					<image :src="baseurl+img" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="null"></view>
		<view class="foot">
			<view class="no" @click="go">暂不退款</view>
			<view class="yes" @click="sub">确认退款</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				app: getApp().globalData,
				baseurl: getApp().globalData.baseurl,
				id: '',
				detail: {},
				array: [],
				index: -1,
				content: '',
				tmpimg: []
			}
		},
		onLoad(e) {
			this.id = e.id
			this.app.post('/api/order/order_info', {
				id: e.id,
			}).then(res => {
				console.log(res)
				this.detail = res;
			})
			this.app.post('/api/order/refund_type').then(res => {
				console.log(res)
				this.array = res;
			})
		},
		methods: {
			sub(){
				if(this.index==-1){
					uni.showToast({
						title: '请选择退款原因',
						icon: "none"
					})
					return
				}
				if(!this.content){
					uni.showToast({
						title: '请输入退款原因',
						icon: "none"
					})
					return
				}
				if(this.tmpimg.length==0){
					uni.showToast({
						title: '请上传凭证',
						icon: "none"
					})
					return
				}
				this.app.post('/api/order/apply_refund', {
					id: this.id,
					yuanyin_id: this.array[this.index].id,
					refund_desc: this.content,
					refund_image: this.tmpimg.join(','),
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: '申请成功',
						icon: "none"
					})
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/voucherPage'
						})
					}, 1500)
				})
			},
			go(){
				uni.switchTab({
					url: '/pages/voucherPage'
				})
			},
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.index = e.detail.value
			},
			shopIndex(id) {
				uni.navigateTo({
					url: '/pages/sc/shaopIndex/shaopIndex?id=' + id
				})
			},
			// 上传图片
			upload() {
				let url = getApp().globalData.baseurl;
				let that = this
				uni.chooseImage({
					count: 3,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));
						res.tempFilePaths.forEach((e, i) => {
							uni.showLoading({
								title: `正在上传\n ${i+1}/${res.tempFilePaths.length}张`
							})
							uni.uploadFile({
								url: url + '/api/common/upload',
								filePath: e,
								name: 'file',
								success: (res) => {
									uni.hideLoading()
									console.log(JSON.parse(res.data))
									that.tmpimg.push(JSON.parse(res.data).data.url)
								}
							})
						})
					}
				})
			},
			// 删除图片
			close(e) {
				this.tmpimg.splice(e, 1)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #EEFCEB !important;
	}

	.box {
		width: 690rpx;
		background-color: #fff;
		border-radius: 20rpx;
		margin-top: 30rpx;
		margin-left: 30rpx;
		padding: 30rpx 0;

		.aa {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.shop {
				display: flex;
				align-items: center;

				image {
					width: 30rpx;
					height: 30rpx;
				}

				view {
					font-size: 26rpx;
					color: #333333;
					margin-left: 10rpx;
					margin-right: 10rpx;

				}
			}

			.state {
				font-size: 26rpx;
				color: #999999;
			}

		}

		.bb {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			margin-top: 20rpx;

			image {
				width: 120rpx;
				height: 120rpx;
				border-radius: 10rpx;
			}

			.detail {
				width: 490rpx;
				height: 120rpx;
				margin-left: 20rpx;
				display: flex;
				flex-wrap: wrap;
				align-items: center;

				.time {
					width: 490rpx;
					font-size: 26rpx;
					color: #666666;
				}

				.two {
					width: 490rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					view {
						font-size: 26rpx;
						color: #666666;
					}
				}
			}
		}

		.cc {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 30rpx;

			.t-name {
				font-size: 30rpx;
				color: #333333;
			}

			.prices {
				font-size: 30rpx;
				color: #333333;
			}
		}
	}

	.tk-box {
		width: 690rpx;
		background-color: #fff;
		border-radius: 20rpx;
		margin-top: 30rpx;
		margin-left: 30rpx;
		padding-bottom: 10rpx;

		.tk-head {
			width: 690rpx;
			height: 106rpx;
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #eee;
			align-items: center;

			.rcon {
				display: flex;
				align-items: center;
				margin-right: 30rpx;

				.mo {
					font-size: 30rpx;
					color: #999999;
				}

				image {
					width: 30rpx;
					height: 30rpx;
				}
			}
		}

		.names {
			font-size: 30rpx;
			color: #333333;
			margin-left: 30rpx;
			line-height: 106rpx;
		}

		.imgs {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			flex-wrap: wrap;

			.pz {
				width: 180rpx;
				height: 180rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background: #F6F6F6;
				border-radius: 10rpx;
				margin-bottom: 20rpx;

				image {
					width: 80rpx;
					height: 80rpx;
				}
			}

			.add-img {
				width: 180rpx;
				height: 180rpx;
				background-color: #f7f7f7;
				border-radius: 15rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-bottom: 20rpx;
				position: relative;
				overflow: hidden;

				.delete {
					display: flex;
					justify-content: center;
					align-items: center;
					position: absolute;
					right: 0rpx;
					top: 0rpx;
					z-index: 999;
					font-size: 45rpx;
					// &::after{
					// 	content: "X";
					// 	color: white;
					// 	font-size: 16rpx;
					// }
				}

				&:nth-child(3n-1) {
					margin-left: 20rpx;
					margin-right: 20rpx;
				}

				image {
					width: 100%;
					height: 100%;
				}

				.icon {
					font-size: 60rpx;
					color: #6f6f6f;
				}
			}
		}

		.ms {
			width: 630rpx;
			margin-left: 30rpx;
			background: #F6F6F6;
			border-radius: 20rpx;
			padding: 30rpx 0;

			textarea {
				width: 570rpx;
				height: 200rpx;
				margin-left: 30rpx;
			}

			view {
				font-size: 24rpx;
				color: #666666;
				text-align: right;
				margin-right: 30rpx;
			}
		}
	}

	.null {
		width: 750rpx;
		height: 200rpx;
	}

	.foot {
		width: 690rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: fixed;
		bottom: 30rpx;
		left: 30rpx;

		.no {
			width: 322rpx;
			height: 80rpx;
			border-radius: 10rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: rgba(132, 189, 0, 0.1);
			font-size: 30rpx;
			color: #84BD00;
		}

		.yes {
			width: 322rpx;
			height: 80rpx;
			border-radius: 10rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: #84BD00;
			font-size: 30rpx;
			color: #FFFFFF;
		}
	}
</style>